/**
 * 包含应用中所有请求接口
 * 每个接口函数的返回值都是Promise对象
*/

import ajax from './ajax';

export const BASE_URL = 'http://120.55.193.14:5000';
// export const BASE_URL = 'http://localhost:5000';

export const IMG_URL = BASE_URL + '/upload/';
// export const IMG_URL = 'http://localhost:5000/upload/';

export const IMG_UPLOAD_URL = BASE_URL + '/manage/img/upload';

export const IMG_FALLBACK = ""



export const reqLogin = (username, password) => ajax(BASE_URL + '/login', { username, password }, 'POST');

export const reqWeather = () => reqCity().then(city => ajax('http://api.jirengu.com/getWeather.php', city));

export const reqCity = () => ajax('http://api.jirengu.com/city.php');

export const reqCategory = (parentId) => ajax(BASE_URL + '/manage/category/list', {parentId});

export const reqUpdateCategory = (categoryId, categoryName) => ajax(BASE_URL + '/manage/category/update', {categoryId, categoryName}, 'POST');

export const reqAddCategory = (parentId, categoryName) => ajax(BASE_URL + '/manage/category/add', {parentId, categoryName}, 'POST');

export const reqCategoryInfo = (categoryId) => ajax(BASE_URL + '/manage/category/info', {categoryId});

export const reqProduct = (pageNum, pageSize) => ajax(BASE_URL + '/manage/product/list', {pageNum, pageSize});

/**
 * 
 * @param {string} type  [productName | productDesc]
 * @returns 
 */
export const reqSearchProduct = ({pageNum, pageSize, keyword, type}) => ajax(BASE_URL + '/manage/product/search', {pageNum, pageSize, [type]: keyword});

export const reqUpdateProductStatus = (productId, status) => ajax(BASE_URL + '/manage/product/updateStatus', {productId, status}, 'POST');

export const reqDeleteIMG = name => ajax(BASE_URL + '/manage/img/delete', {name}, 'POST');
// export const reqUploadIMG = image => ajax(BASE_URL + '/manage/img/upload', {image}, 'POST');

/**
 * 
 * @param {*} data {_id, categoryId, pCategoryId, name, desc, price, detail, imgs}
 * @returns 
 */
export const reqUpdateProduct = data => ajax(BASE_URL + '/manage/product/update', data, 'POST');
/**
 * 
 * @param {*} data {categoryId, pCategoryId, name, desc, price, detail, imgs}
 * @returns 
 */
export const reqAddProduct = data => ajax(BASE_URL + '/manage/product/add', data, 'POST');

export const reqRoleList = () => ajax(BASE_URL + '/manage/role/list');

export const reqRoleAdd = roleName => ajax(BASE_URL + '/manage/role/add', {roleName}, 'POST');
/**
 * 
 * @param {*} data {_id, menus, auth_time, auth_name}
 * @returns 
 */
export const reqRoleUpdate = (data) => ajax(BASE_URL + '/manage/role/update', data, 'POST');

export const reqUserList = () => ajax(BASE_URL + '/manage/user/list');

export const reqDeleteUser = (userId) => ajax(BASE_URL + '/manage/user/delete', {userId}, 'POST');

/**
 * 
 * @param {*} user {username, password[, phone, email, role_id]}
 * @returns 
 */
export const reqAddUser = user => ajax(BASE_URL + '/manage/user/add', user, 'POST');

/**
 * 
 * @param {*} user {_id[, password, phone, email, role_id]}
 * @returns 
 */
export const reqUpdateUser = user => ajax(BASE_URL + '/manage/user/update', user, 'POST');